<?php

require_once("ugovori.class.php");
/**
 * Izveštaji ugovora
 * @author Voislav Jovanović - voislavj@gmail.com
 *
 */
class ugovori_report extends ugovori {
	
	var $db;
	var $debtMinPerc = 0.02;
	
	function ugovori_report(){
		
		$this->db = new db_functions();
		
		$mod = getFromInput("mod");
		$rep = getFromInput("rep");
		switch ($mod){
			case "book":
				$this->BookReport();
				break;
			case "stat":
				$this->StatReport();
				break;
			case "editor":
				$this->EditorReport();
				break;
			case "debt":
				switch ($rep){
					case "dugovanja":
						$this->DebtReportDugovanja();
						break;
					default:
						$this->DebtReport();
						break;
				}
				break;
		}
		$action=getFromInput("a2");
		switch ($action)
		{
			case "pozivaoci":
				$ugovor=getFromInput("ugo");
				$user=getFromInput("user");
				$this->duzniciSavePozivalac($ugovor,$user);
				break;
		}
	}
	
	//-------------------------------------------------------------
	
	function StatReport(){
		$delim = "<br />";
		
		$f_page = (int) getFromInput("page");
		$f_page = $f_page>0?$f_page:1;
		
		$f_date_start = getFromInput("filter_date_start");
		$f_date_end = getFromInput("filter_date_end");
		$f_dopis_type = getFromInput("filter_dopis_type");
		$f_dopis_result = getFromInput("filter_dopis_result");
		$f_ne_obnova = getFromInput("filter_ne_obnova");
		$f_tip_ugovora = getFromInput("filter_tip_ugovora");
		$f_kampanja = getFromInput("filter_kampanja");
		$f_radnik = getFromInput("filter_radnik");
		
		
		$f_storno = getFromInput("filter_storno");
		
		$filter_query = "a=real_report&mod=stat"
					  ."&filter_date_start=$f_date_start"
					  ."&filter_date_end=$f_date_end"
					  ."&filter_dopis_type=$f_dopis_type"
					  ."&filter_dopis_result=$f_dopis_result"
					  ."&filter_ne_obnova=$f_ne_obnova"
					  ."&filter_kampanja=$f_kampanja"
					  ."&filter_radnik=$f_radnik"
					  ."&filter_storno=$f_storno"
					  ."&filter_tip_ugovora=$f_tip_ugovora";
		$this->assign("filter_query",$filter_query);
		
		$f_ne_obnova = (int)$f_ne_obnova;
		$f_storno = $f_storno=="on"?true:false;
		$polja = ugovori::get_UgovorTipovi($f_tip_ugovora);
			$this->assign("tip_ugovora_id",$polja["polje_id"]);
			$this->assign("tip_ugovora_name",$polja["polje_ime"]);
			$this->assign("tip_ugovora_selected",$polja["polje_selected"]);
		
		$this->assign("checked_storno",$f_storno?" checked":"");
		
		$this->assign("filter_date_start",$f_date_start);
		$this->assign("filter_date_end",$f_date_end);
		
		$f_date_start = date2int($f_date_start);
		$f_date_end = date2int($f_date_end);
		
		$polja = ugovori::get_DopisTipovi($f_dopis_type);
			$this->assign("dopis_type_id",$polja["polje_id"]);
			$this->assign("dopis_type_name",$polja["polje_ime"]);
			$this->assign("dopis_type_selected",$polja["polje_selected"]);
		$polja = ugovori::get_DopisIshodi($f_dopis_result);
			$this->assign("dopis_result_id",$polja["polje_id"]);
			$this->assign("dopis_result_name",$polja["polje_ime"]);
			$this->assign("dopis_result_selected",$polja["polje_selected"]);
		$polja = $this->get_Kampanje($f_kampanja);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("kampanja_selected",$polja["polje_selected"]);
		$polja = $this->get_Radnici($f_radnik,null,array(2));
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		
		switch ($f_ne_obnova){
			case 1:
				$this->assign("selected_ne_obnova_da"," selected");
				break;
			case 2:
				$this->assign("selected_ne_obnova_ne"," selected");
				break;
		}
		
		if (($f_date_start>0 && $f_date_end>0) || $f_dopis_type>0 || $f_dopis_result>0 || $f_ne_obnova>0 || (strlen($f_kampanja) && $f_kampanja>=0)) {
		
			$q_select = "SELECT ugovori.id, ugovori.broj, ugovori.datum, ugovori.ne_obnova, "
					   ."ugovori.napomena, ugovori.iznos_bruto, ugovori.saradnik";
			$q_from = "FROM ugovori";
			$q_where = "WHERE 1";
			
			if ($f_storno && $f_tip_ugovora!=5) {
				$q_where .= " AND ugovori.tip_ugovora != 5";
			}
			
			if ($f_tip_ugovora>0) {
				$q_where .= " AND ugovori.tip_ugovora=$f_tip_ugovora";
			}
			
			if ($f_radnik>0) {
				$q_where .= " AND ugovori.saradnik=$f_radnik";
			}
			
			if ($f_date_start>0 && $f_date_end>0) {
				$q_where .= " AND (ugovori.datum>='".date("Y-m-d",$f_date_start)."' AND ugovori.datum<='".date("Y-m-d",$f_date_end)."')";
			}
			
			if ($f_dopis_type>0 || $f_dopis_result>0) {
				$q_from .= ", dopisi";
				$q_where .= " AND (ugovori.id=dopisi.ugovor_id)";
				if ($f_dopis_type>0) {
					$q_where .= " AND (dopisi.tip=$f_dopis_type) ";
				}
				if ($f_dopis_result>0) {
					$q_where .= " AND (dopisi.ishod=$f_dopis_result)";
				}
			}
			
			if ($f_ne_obnova>0) {
				$q_where .= " AND ugovori.ne_obnova=".($f_ne_obnova>1?0:1);
			}
			if ($f_kampanja>=0) {
				$q_from .= ", ugovori_stavke";
				$q_where .= " AND (ugovori.id=ugovori_stavke.ugovor_id AND ugovori_stavke.kampanja=$f_kampanja)";
				$q_group = " GROUP BY ugovori.id";
			}
			
			$q_limit = "LIMIT ".(($f_page-1)*PAGE_LENGTH).", ".PAGE_LENGTH;
			
			$ukupno = count($this->db->select2("$q_select $q_from $q_where $q_group"));
			$num_pages = ceil($ukupno/PAGE_LENGTH);
			
			$sql = "SELECT DISTINCT ugovori.komitent $q_from $q_where";
			//echo $sql;
			$ukupno_komitenata = count($this->db->select2($sql));
			
			$q = "$q_select $q_from $q_where $q_group ORDER BY ugovori.datum DESC $q_limit";
			//echo $q;
			
			// STRANE
			for ($i=0; $i<$num_pages; $i++){
				$page[$i] = $i+1;
				if ($i == ($f_page-1)) {
					$page_curr[$i] = " id=\"current\"";
				}else{
					$page_curr[$i] = "";
				}
			}
			$this->assign("page",$page);
			$this->assign("current_page",$page_curr);
			
			$res = $this->db->select2($q);
			if ($res !== false) {
				$komids = array();
				foreach ($res as $k=>$ugovor){
					$ugoid = $ugovor["id"];
					
					$id[$k] = $ugoid;
					$broj[$k] = $ugovor["broj"];
					$datum[$k] = datetime2str($ugovor["datum"]);
					$neobnova[$k] = $ugovor["ne_obnova"]>0?"DA":"";
					$napomena[$k] = nl2br($ugovor["napomena"]);
					
					$vrednost[$k] = number_format($ugovor["iznos_bruto"],2,",",".");
					$uplate = (float)$this->db->getOne("SELECT SUM(iznos) FROM ugovori_otplate WHERE ugoid=$ugoid");
					$dug[$k] = ((float)$ugovor["iznos_bruto"])-($uplate);
					$dug[$k] = number_format($dug[$k],2,",",".");
					$uplata[$k] = number_format($uplate,2,",",".");
					$komercijalista[$k] = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$ugovor["saradnik"]." LIMIT 1");
					
					// kampanja
					$q = "SELECT DISTINCT ugovori_stavke.kampanja, kampanje.kampanja_naziv "
						."FROM ugovori_stavke, kampanje "
						."WHERE ugovori_stavke.kampanja=kampanje.kampanja_id "
						."AND ugovori_stavke.ugovor_id=$ugoid";
					$kampanje = $this->db->select2($q);
					$kampanja[$k] = "";
					if ($kampanje !== false) {
						foreach ($kampanje as $kam){
							$kampanja[$k] .= $delim.$kam["kampanja_naziv"];
						}
						$kampanja[$k] = substr($kampanja[$k],strlen($delim));
					}
					
					// dopis tip
					$q = "SELECT dopisi_tipovi.name as doptype, dopisi_ishodi.name as dopresult "
						."FROM dopisi, dopisi_tipovi, dopisi_ishodi "
						."WHERE dopisi.tip=dopisi_tipovi.tip_dopisa_id "
						."AND dopisi.ishod=dopisi_ishodi.dopis_ishod_id "
						."AND dopisi.ugovor_id=$ugoid "
						."ORDER BY dopisi.radnik_time";
					$dopisi = $this->db->select2($q);
					$dopis[$k] = "";
					if ($dopisi !== false) {
						foreach ($dopisi as $dop){
							$dopis[$k] .= $delim.$dop["doptype"]."=>".$dop["dopresult"];
						}
						$dopis[$k] = substr($dopis[$k],strlen($delim));
					}
					
					// komitenti
					$q = "SELECT DISTINCT komitent_id, komitent_name "
						."FROM komitenti, ugovori_stavke "
						."WHERE komitenti.komitent_id=ugovori_stavke.komitent "
						."AND ugovori_stavke.ugovor_id=$ugoid";
					$komitenti = $this->db->select2($q);
					$komitent[$k] = "";
					if ($komitenti !== false) {
						foreach ($komitenti as $kom){
							$komids[] = $kom["komitent_id"];
							$komitent[$k] .= $delim.$kom["komitent_id"].".".$kom["komitent_name"];
						}
						$komitent[$k] = substr($komitent[$k],strlen($delim));
					}
					
					// stavke
					$stavke = $this->get_UgovorStavke($ugoid);
					if ($stavke) {
						$stavka[$k] = implode($delim,$stavke);
					}else{
						$stavka[$k] = "";
					}
					
				}
				
				$komids = array_unique($komids);
				$this->assign("ukupno",$ukupno);
				$this->assign("ukupno_komitenata",$ukupno_komitenata);
				
				$this->assign("id",$id);
				$this->assign("broj",$broj);
				$this->assign("datum",$datum);
				$this->assign("ne_obnova",$neobnova);
				$this->assign("napomena",$napomena);
				$this->assign("kampanja",$kampanja);
				$this->assign("komercijalista",$komercijalista);
				$this->assign("dopis",$dopis);
				$this->assign("komitent",$komitent);
				$this->assign("stavke",$stavka);
				$this->assign("vrednost",$vrednost);
				$this->assign("uplate",$uplata);
				$this->assign("dug",$dug);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","--> Nema rezultata");
			}
		}
		
		
		echo $this->parseTemplate("ugovori/reports/stat.tpl");
	}
	
	//-------------------------------------------------------------
	
	function EditorReport(){
		
		$page = (int)getFromInput("page"); $page=$page>0?$page:1;
		
		// get filter data
		$Form = array();
		foreach ($_POST as $k=>$v){
			if (preg_match('/^f_/',$k)) {
				if (preg_match('/_start$|_end$/',$k) && is_valid_date($v)) {
					$Form[$k]["type"] = "date";
					$Form[$k]["value"] = date2int($v);
				}else if(preg_match('/_bez$/',$k)){
					$Form[$k]["type"] = "boolean";
					$Form[$k]["value"] = ($v=="on")?true:false;
				}else{
					$Form[$k]["type"] = "string";
					$Form[$k]["value"] = $v;
				}
				$$k = $Form[$k]["value"];
			}
		}
		// variable assigning
		foreach ($Form as $name=>$data){
			if ($data["type"]=="date") {
				$this->assign($name,int2date($data["value"]));
			}else if($data["type"]=="boolean"){
				$this->assign($name."_checked",$data["value"]?" checked":"");
			}else{
				$this->assign($name,$data["value"]);
			}
		}
		$polja = $this->get_Kampanje($Form["f_kampanja"]["value"]);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("kampanja_selected",$polja["polje_selected"]);
		$polja = $this->get_Radnici($Form["f_radnik"]["value"],null,array(2));
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		switch ($Form["f_placeno"]["value"]){
			case "0":
				$this->assign("placeno_ne_selected"," selected");
				break;
			case "1":
				$this->assign("placeno_delimicno_selected"," selected");
				break;
			case "2":
				$this->assign("placeno_da_selected"," selected");
				break;
		}
		$polja = $this->get_TipoviUlaza($Form["f_mat_tip_ulaza"]["value"]);
			$this->assign("tip_ulaza_id",$polja["polje_id"]);
			$this->assign("tip_ulaza_name",$polja["polje_ime"]);
			$this->assign("tip_ulaza_selected",$polja["polje_selected"]);
		$polja = $this->get_TipoviMaterijala($Form["f_mat_tip"]["value"]);
			$this->assign("tip_mat_id",$polja["polje_id"]);
			$this->assign("tip_mat_name",$polja["polje_ime"]);
			$this->assign("tip_mat_selected",$polja["polje_selected"]);
		switch ($Form["f_mat_upotrebljiv"]["value"]){
			case "1":
				$this->assign("f_mat_upo_ne"," selected");
				break;
			case "2":
				$this->assign("f_mat_upo_da"," selected");
				break;
		}
		$polja = $this->get_RadniciByType(array(32),$Form["f_act_editor"]["value"]);
			$this->assign("editor_id",$polja["polje_id"]);
			$this->assign("editor_ime",$polja["polje_ime"]);
			$this->assign("editor_prezime",$polja["polje_prezime"]);
			$this->assign("editor_selected",$polja["polje_selected"]);
		$polja = $this->get_RadniciByType(array(12,14,15),$Form["f_act_designer"]["value"]);
			$this->assign("designer_id",$polja["polje_id"]);
			$this->assign("designer_ime",$polja["polje_ime"]);
			$this->assign("designer_prezime",$polja["polje_prezime"]);
			$this->assign("designer_selected",$polja["polje_selected"]);
		switch ($Form["f_act_uradjen"]["value"]){
			case "1":
				$this->assign("f_act_uradjen_ne"," selected");
				break;
			case "2":
				$this->assign("f_act_uradjen_da"," selected");
				break;
		}
		
		/*$uslov = false;
		foreach ($Form as $name=>$data){
			if ($data["type"]=="date" && preg_match('/_start$/',$name)) {
				$endname = preg_replace('/_start$/',"_end",$name);
				
				if ($data["value"]>0 && $Form[$endname]["value"]>0 && $data["value"]<=$Form[$endname]["value"]) {
					$uslov = true;
					break;
				}
			}
		}*/
		$uslov = $f_kampanja>=0 && $f_kampanja!==null;
		
		$filter_query =	  "";
		foreach ($Form as $name=>$data){
			$filter_query .= "&$name=";
			if ($data["type"]=="date") {
				$filter_query .= int2date($data["value"]);
			}else{
				$filter_query .= $data["value"];
			}
		}
		$this->assign("filter_query",substr($filter_query,1));
		
		if ($uslov) {
			
			// queries
			$q_select = "SELECT u.id as ugovorid, u.broj as ugovorbroj, u.saradnik, "
					   ."u.komitent, u.iznos_bruto as ugovorvrednost, IFNULL(SUM(uo.iznos),0) as `otplaceno`, "
					   ."us.artikl as stavkaartikl, us.paket, "
					   ."utr.artikl as tokartikl, "
					   //."tm.date_ulaz, tm.tip_ulaz, tm.tip_materijal, tm.upotrebljiv, "
					   //."uta.*, "
					   ."utr.* ";
			$q_from = "FROM ugovori u LEFT JOIN ugovori_otplate uo ON u.id=uo.ugoid , ugovori_stavke us, ugovori_tok_realizacije utr ";
			$q_where = "WHERE u.id=us.ugovor_id "
					  ."AND us.stavka_id=utr.stavka ";
			$q_group = "GROUP BY u.id, us.stavka_id, utr.tok_id";
			$q_order = "ORDER BY u.datum DESC";
			$q_limit = "LIMIT ".(($page-1)*PAGE_LENGTH).", ".PAGE_LENGTH;
			
			// MODIFY QUERIES BY FILTER PARAMS
			
			// kampanja
			if ($f_kampanja>0) {
				$q_where .= " AND us.kampanja=$f_kampanja";
			}
			
			if ($f_radnik>0) {
				$q_where .= " AND u.saradnik=$f_radnik";
			}
			
			// placeno
			switch ((int)$f_placeno){
				case 0:
					$q_group .= " HAVING (otplaceno<1)";
					break;
				case 1:
					$q_group .= " HAVING (otplaceno>0 AND otplaceno<u.iznos_bruto)";
					break;
				case 2:
					$q_group .= " HAVING (otplaceno>=u.iznos_bruto)";
					break;
			}
			// datum poslato
			if (!$f_poslato_bez) {
				if ((int)$f_poslato_start>0 && (int)$f_poslato_end>0 && $f_poslato_end>=$f_poslato_start) {
					$q_where .= " AND (utr.datum_poslato>=$f_poslato_start AND utr.datum_poslato<=$f_poslato_end)";
				}
			}else{
				$q_where .= " AND utr.datum_poslato<1";
			}
			// datum obavesten
			if (!$f_obavesten_bez) {
				if ((int)$f_obavesten_start>0 && (int)$f_obavesten_end>0 && $f_obavesten_end>=$f_obavesten_start) {
					$q_where .= " AND (utr.datum_obavesten>=$f_obavesten_start AND utr.datum_obavesten<=$f_obavesten_end)";
				}
			}else{
				$q_where .= " AND utr.datum_obavesten<1";
			}
			// datum potvrda
			if (!$f_potvrda_bez) {
				if ((int)$f_potvrda_start>0 && (int)$f_potvrda_end>0 && $f_potvrda_end>=$f_potvrda_start) {
					$q_where .= " AND (utr.datum_potvrda>=$f_potvrda_start AND utr.datum_potvrda<=$f_potvrda_end)";
				}
			}else{
				$q_where .= " AND utr.datum_potvrda<1";
			}
			
			$ask_materijal = (!$f_mat_date_bez && (int)$f_mat_date_start>0 && (int)$f_mat_date_end>0 && $f_mat_date_end>=$f_mat_date_start) ||
							 (!$f_mat_ocek_date_bez && (int)$f_mat_ocek_date_start>0 && (int)$f_mat_ocek_date_end>0 && $f_mat_ocek_date_end>=$f_mat_ocek_date_start) ||
							 ($f_mat_tip_ulaza>=0) ||
							 ($f_mat_tip>=0) ||
							 ($f_mat_upotrebljiv>0);
			
			//var_dump($ask_materijal);
			if ($ask_materijal) {
				
				$q_from .= ", tok_materijali tm";
				
				$q_where .= " AND utr.tok_id=tm.tok ";
				$q_where .= " AND tm.id=("
								."SELECT tok_materijali.id "
								."FROM tok_materijali "
								."WHERE tok_materijali.tok=utr.tok_id "
								."ORDER BY tok_materijali.date_ulaz DESC "
								."LIMIT 1"
							.") ";
			}
			
			// materijal usao
			if (!$f_mat_date_bez) {
				if ((int)$f_mat_date_start>0 && (int)$f_mat_date_end>0 && $f_mat_date_end>=$f_mat_date_start) {
					$q_where .= " AND (tm.date_ulaz>=$f_mat_date_start AND tm.date_ulaz<=$f_mat_date_end)";
				}
			}else{
				$q_where .= " AND 0=(SELECT count(tok) FROM tok_materijali WHERE tok_materijali.tok=utr.tok_id)";
			}
			// materijal se ocekuje
			if (!$f_mat_ocek_date_bez) {	
				if ((int)$f_mat_ocek_date_start>0 && (int)$f_mat_ocek_date_end>0 && $f_mat_ocek_date_end>=$f_mat_ocek_date_start) {
					$q_where .= " AND (utr.materijal_date_ocek>=$f_mat_ocek_date_start AND utr.materijal_date_ocek<=$f_mat_ocek_date_end)";
				}
			}else{
				$q_where .= " AND utr.materijal_date_ocek<1";
			}
			// materijal tip ulaza
			if ($f_mat_tip_ulaza>=0) {
				$q_where .= " AND (tm.tip_ulaz=$f_mat_tip_ulaza)";
			}
			// materijal tip
			if ($f_mat_tip>=0) {
				$q_where .= " AND (tm.tip_materijal=$f_mat_tip)";
			}
			// materijal upotrebljiv
			if ($f_mat_upotrebljiv>0) {
				$q_where .= " AND tm.upotrebljiv=".($f_mat_upotrebljiv-1)." ";
			}
			
			
			$ask_action = $f_act_uzeo_bez || 
						  (!$f_act_uzeo_bez && (int)$f_act_uzeo_start>0 && (int)$f_act_uzeo_end>0 && $f_act_uzeo_end>=$f_act_uzeo_start) ||
						  (!$f_act_vratio_bez && (int)$f_act_vratio_start>0 && (int)$f_act_vratio_end>0 && $f_act_vratio_end>=$f_act_vratio_start) ||
						  ($f_act_editor>0) || 
						  ($f_act_designer>0) || 
						  ($f_act_uradjen>0);
			
			if ($ask_action) {
				$q_from .= ", ugovori_tok_actions uta";
				$q_where .= " AND utr.tok_id=uta.tok ";
				$q_where .= " AND uta.action_id=("
								."SELECT ugovori_tok_actions.action_id "
								."FROM ugovori_tok_actions "
								."WHERE ugovori_tok_actions.tok=utr.tok_id "
								."ORDER BY ugovori_tok_actions.action_id DESC "
								."LIMIT 1"
							.") ";
			}
			
			
			// akcija dizajnera preuzeo
			if (!$f_act_uzeo_bez) {
				if ((int)$f_act_uzeo_start>0 && (int)$f_act_uzeo_end>0 && $f_act_uzeo_end>=$f_act_uzeo_start) {
					$q_where .= " AND (uta.date_uzimanja>=$f_act_uzeo_start AND uta.date_uzimanja<=$f_act_uzeo_end)";
				}
			}else{
				$q_where .= " AND 0=(SELECT count(action_id) FROM ugovori_tok_actions WHERE ugovori_tok_actions.tok=utr.tok_id)";
			}
			// akcija dizajnera vratio
			if (!$f_act_vratio_bez) {
				if ((int)$f_act_vratio_start>0 && (int)$f_act_vratio_end>0 && $f_act_vratio_end>=$f_act_vratio_start) {
					$q_where .= " AND (uta.date_vracanja>=$f_act_vratio_start AND uta.date_vracanja<=$f_act_vratio_end)";
				}
			}else{
				$q_where .= " AND 0<(SELECT count(action_id) FROM ugovori_tok_actions WHERE ugovori_tok_actions.tok=utr.tok_id AND ugovori_tok_actions.date_vracanja<1)";
			}
			// akcija dizajnera urednik
			if ($f_act_editor>0) {
				$q_where .= " AND (uta.urednik=$f_act_editor)";
			}
			// akcija dizajnera dizajner
			if ($f_act_designer>0) {
				$q_where .= " AND (uta.designer=$f_act_designer)";
			}
			// akcija dizajnera uradjeno
			if ($f_act_uradjen>0) {
				$q_where .= " AND (uta.done=".($f_act_uradjen-1).")";
			}
			// ukupno zapisa
			$q = "$q_select $q_from $q_where $q_group"; //echo $q;
			$ukupno = $this->db->select2($q);
			$ukupno = $ukupno!==false?count($ukupno):0;
			if ($ukupno>0) {
				// main query
				$q = "$q_select $q_from $q_where $q_group $q_order $q_limit"; //echo $q;
				$res = $this->db->select2($q);
				if ($res !== false) {
					$prevugoid = 0;
					$prevclass = "even";
					
					foreach ($res as $k=>$row){
						
						$className[$k] = $row["ugovorid"]==$prevugoid?$prevclass:$prevclass=="even"?"odd":"even";
						$prevclass = $className[$k];
						$prevugoid = $row["ugovorid"];
						
						$ugoids[$k] = $row["ugovorid"];
						$ugobrs[$k] = $row["ugovorbroj"];
						$komids[$k] = $row["komitent"];
						
						$komnames[$k] = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$row["komitent"]." LIMIT 1");
						
						$komercijalista[$k] = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$row["saradnik"]." LIMIT 1");
						
						// stavka
						$paket = ((int)$row["paket"])>0?true:false;
						if ($paket) {
							$a = $this->get_CenePaket($row["stavkaartikl"]);
							$art = $a["name"];
							
							$aart = $this->get_CeneArtikl($row["tokartikl"]);
							$art .= "<span class=\"info\"><br />".$aart["art_name"]."</span>";
						}else{
							$art = $this->get_CeneArtikl($row["stavkaartikl"]);
							$art = implode(" - ",$art["art_full_name"]);
						}
						$stavke[$k] = $art;
						
						// materijal
						$materijal_html = "";
						$sql = "SELECT * FROM tok_materijali tm WHERE tok=".$row["tok_id"];
							// materijal usao
							if ((int)$f_mat_date_start>0 && (int)$f_mat_date_end>0 && $f_mat_date_end>=$f_mat_date_start) {
								$sql .= " AND (tm.date_ulaz>=$f_mat_date_start AND tm.date_ulaz<=$f_mat_date_end)";
							}
							// materijal tip ulaza
							if ($f_mat_tip_ulaza>=0) {
								$sql .= " AND (tm.tip_ulaz=$f_mat_tip_ulaza)";
							}
							// materijal tip
							if ($f_mat_tip>=0) {
								$sql .= " AND (tm.tip_materijal=$f_mat_tip)";
							}
							// materijal upotrebljiv
							if ($f_mat_upotrebljiv>0) {
								$sql .= " AND (tm.upotrebljiv=".($f_mat_upotrebljiv-1).")";
							}
						$sql .= " ORDER BY date_ulaz DESC";
						$mat_res = $this->db->select2($sql);
						if ($mat_res !== false) {
							foreach ($mat_res as $m=>$n){
								$date_ulaz[$m] = int2date($n["date_ulaz"]);
								
								$types = explode(";",$n["tip_materijal"]);
								$tipovi[$m] = "";
								if (count($types)) {
									foreach ($types as $mat_type_id){
										if (strlen(trim($mat_type_id))>0) {
											$sql = "SELECT mattip_name FROM materijal_tipovi WHERE mattip_id=$mat_type_id LIMIT 1";
											$tipovi[$m] .= ", ".$this->db->getOne($sql);
										}
									}
									$tipovi[$m] = substr($tipovi[$m],2);
								}
								
								$sql = "SELECT tip_ulaz_name FROM materijal_tip_ulaza WHERE tip_ulaz_id=".$n["tip_ulaz"]." LIMIT 1";
								$tip_ulaza[$m] = $this->db->getOne($sql);
								
								$upotrebljiv[$m] = $n["upotrebljiv"]>0?"DA":"NE";
							}
							$this->assign("tipovi",$tipovi);
							$this->assign("date_ulaz",$date_ulaz);
							$this->assign("tip_ulaza",$tip_ulaza);
							$this->assign("upotrebljiv",$upotrebljiv);
							
							$materijal_html = $this->parseTemplate("ugovori/reports/editor_materijal.tpl");
						}
						$materijal[$k] = $materijal_html;
						
						// akcije dizajnera
						$actions_html = "";
						$sql = "SELECT * FROM ugovori_tok_actions uta WHERE tok=".$row["tok_id"];
							// akcija dizajnera vratio
							if ((int)$f_act_vratio_start>0 && (int)$f_act_vratio_end>0 && $f_act_vratio_end>=$f_act_vratio_start) {
								$sql .= " AND (uta.date_vracanja>=$f_act_vratio_start AND uta.date_vracanja<=$f_act_vratio_end)";
							}
							// akcija dizajnera urednik
							if ($f_act_editor>0) {
								$sql .= " AND (uta.urednik=$f_act_editor)";
							}
							// akcija dizajnera dizajner
							if ($f_act_designer>0) {
								$sql .= " AND (uta.designer=$f_act_designer)";
							}
							// akcija dizajnera uradjeno
							if ($f_act_uradjen>0) {
								$sql .= " AND (uta.done=".($f_act_uradjen-1).")";
							}
							$sql .= " ORDER BY date_uzimanja DESC";
						$act_res = $this->db->select2($sql);
						if ($act_res !== false) {
							foreach ($act_res as $m=>$n){
								$date_uzimanja[$m] = int2date($n["date_uzimanja"]);
								$date_vracanja[$m] = int2date($n["date_vracanja"]);
								
								$sql = "SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$n["urednik"]." LIMIT 1";
								$urednik[$m] = $this->db->getOne($sql);
								
								$sql = "SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$n["designer"]." LIMIT 1";
								$designer[$m] = $this->db->getOne($sql);
								
								$designer_note[$m] = $n["designer_note"];
								$uradjeno[$m] = $n["done"]>0?"DA":"NE";
								$napomena[$m] = $n["napomena"];
							}
							$this->assign("date_uzimanja",$date_uzimanja);
							$this->assign("date_vracanja",$date_vracanja);
							$this->assign("urednik",$urednik);
							$this->assign("designer",$designer);
							$this->assign("designer_note",$designer_note);
							$this->assign("uradjeno",$uradjeno);
							$this->assign("napomena",$napomena);
							
							$actions_html = $this->parseTemplate("ugovori/reports/editor_actions.tpl");
						}
						$actions[$k] = $actions_html;
						
						$acts[$k] = $act_date_uz;
						
					}
					$this->assign("class",$className);
					$this->assign("ugovor_id",$ugoids);
					$this->assign("ugovor_broj",$ugobrs);
					$this->assign("komitent_id",$komids);
					$this->assign("komitent_name",$komnames);
					$this->assign("komercijalista",$komercijalista);
					$this->assign("stavka",$stavke);
					$this->assign("materijal",$materijal);
					$this->assign("akcije_dizajnera",$actions);
				}
				
				
				// filter po stranama
				$this->assign("ukupno",$ukupno);
				$this->assign("current_page",$page);
				$numpages = ceil($ukupno/PAGE_LENGTH);
				$this->assign("numpages",$numpages);
				if ($numpages<=20) {
					$pagestart = 0;
					$pageend = $numpages;
				}else{
					$pagestart = $page-10; $pagestart = $pagestart>0?$pagestart:0;
					$pageend = $page+10; $pageend = $pageend>$numpages?$numpages:$pageend;
				}
				for ($i=$pagestart,$j=0; $i<$pageend; $i++,$j++){
					$pages[$j] = $i+1;
					$pages_current[$j] = ($i+1)==$page?" id=\"current\"":"";
				}
				$this->assign("page",$pages);
				$this->assign("page_current",$pages_current);
				
			}else{
				// nema rezultata pretrage
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema rezultata");
			}
			
		}else{
			// nista nije filtrirano
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Odaberite kriterijume pretrage");
		}
		
		
		
		
		echo $this->parseTemplate("ugovori/reports/editor.tpl");
	}
	
	//-------------------------------------------------------------
	
	function BookReport(){
		
		$BookGroupID = 1;
		$UgovorTypesSpecial = array(2,3,4);
		
		// get filter data
		$kampanja = getFromInput("kampanja");
		$date_start = date2int(getFromInput("date_start"));
		$date_end = date2int(getFromInput("date_end"));
		
		// assign filter data
		$polja = $this->get_Kampanje($kampanja);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("kampanja_selected",$polja["polje_selected"]);
		$this->assign("date_start",int2date($date_start));
		$this->assign("date_end",int2date($date_end));
		
		// do the report
		if ($kampanja>0) {
			$q = "SELECT u.id, u.broj, u.komitent as ugokomitent, u.tip_ugovora, us.komitent, us.paket, us.artikl, us.kolicina, us.delatnost "
				."FROM ugovori u, ugovori_stavke us "
				."WHERE u.id=us.ugovor_id "
				."AND us.kampanja=$kampanja "
				."AND u.tip_ugovora!=5 "
				."AND (us.kolicina>0 OR u.tip_ugovora IN (".implode(",",$UgovorTypesSpecial)."))";
			if ($date_start>0 && $date_end>0 && $date_start<=$date_end) {
				$q .= " AND u.datum>='".date("Y-m-d",$date_start)."' AND u.datum<'".date("Y-m-d",$date_end+DAY)."'";
			}
			$q .= "ORDER BY u.id";
			$res = $this->db->select2($q);
			if ($res !== false) {
				
				$sql = "SELECT a.art_id, a.art_name, ask.koeficijent "
					  ."FROM artikli a LEFT JOIN artikli_strana_koeficijent ask ON a.art_id=ask.art_id "
					  ."WHERE a.art_grupa_id=$BookGroupID";
				$res1 = $this->db->select2($sql);
				if ($res1!==false) {
					foreach ($res1 as $m=>$n){
						$_artikli[$n["art_id"]] = $n["art_name"];
						$_artikli_koeficijenti[$n["art_id"]] = $n["koeficijent"];
					}
				}
				
				$res1 = $this->db->select2("SELECT * FROM artikli_tipovi");
				if ($res1 !== false) {
					foreach ($res1 as $m=>$n){
						$_artikli_tipovi[$n["arttip_id"]] = $n["arttip_name"];
					}
				}
				
				$res1 = $this->db->select2("SELECT * FROM artikli_boje");
				if ($res1 !== false) {
					foreach ($res1 as $m=>$n){
						$_artikli_boje[$n["art_boja_id"]] = $n["art_boja_code"];
					}
				}
				
				$data = array();
				foreach ($res as $k=>$row){
					$ugoid = $row["id"];
					$ugobroj = $row["broj"];
					$komid = $row["komitent"]>0?$row["komitent"]:$row["ugokomitent"];
					$paket = $row["paket"]>0?true:false;
					$cenid = $row["artikl"];
					
					if ($row["kolicina"]==0 && in_array((int)$row["tip_ugovora"],$UgovorTypesSpecial)) {
						$row["kolicina"]=1;
						//echo "$ugobroj<br />";
					}
					
					$artikli = $artikli_mods = array();
					if ($paket) {
						$pak = $this->get_CenePaket($cenid);
						if ($pak) {
							foreach ($pak["artikli"] as $art){
								if ((int)$art["group"]==$BookGroupID) {
									$artikli[] = $art["id"];
									
									$sql = "SELECT c.cena_art_tip, at.arttip_name, c.cena_art_boja, ab.art_boja_code "
										  ."FROM (cene c LEFT JOIN artikli_tipovi at ON c.cena_art_tip=at.arttip_id) "
										  	."LEFT JOIN artikli_boje ab ON c.cena_art_boja=ab.art_boja_id "
										  ."WHERE c.cena_id=".$art["cena_id"]." LIMIT 1";
									list($tipid,$tipname,$bojaid,$bojaname) = $this->db->getRow($sql);
									$artikli_mods[] = array(
										"tip_id" => $tipid,
										"tip_name" => $tipname,
										"boja_id" => $$bojaid,
										"boja_name" => $$bojaname
									);
								}
							}
						}
					}else{
						$art = $this->get_CeneArtikl($cenid);
						if ((int)$art["art_group"]==$BookGroupID) {
							$artikli[] = $art["art_id"];
							$artikli_mods[] = array(
								"tip_id" => $art["art_tip_id"]>0?(int)$art["art_tip_id"]:0,
								"tip_name" => $art["art_tip_name"],
								"boja_id" => $art["art_boja_id"]>0?(int)$art["art_boja_id"]:0,
								"boja_name" => $art["art_boja_name"]
							);
						}
					}
					if (count($artikli)>0) {
						$key = count($data);
						$data[$key]["id"] = $ugoid;
						$data[$key]["broj"] = $ugobroj;
						$data[$key]["komitent"] = $komid;
						$data[$key]["artikli"] = $artikli;
						$data[$key]["artikli_mods"] = $artikli_mods;
						$data[$key]["delatnost"] = $row["delatnost"];
						$data[$key]["kolicina"] = $row["kolicina"];
					}
				}
				$ugovori = $komitenti = $artikli = $artikli_ugovori = $artikli_modovi =
				$delatnosti = $delatnosti_names = $delatnosti_artikli = array();
				
				// po ugovoru
				foreach ($data as $k=>$v){
					
					// sum
					$ugovori[] = $v["id"];
					$komitenti[] = $v["komitent"];
					
					// data assign
					$ugovorid[$k] = $v["id"];
					$ugovorbroj[$k] = $v["broj"];
					$komitentid[$k] = $v["komitent"];
					$komname = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$v["komitent"]." LIMIT 1");
					$komitentname[$k] = $komname;
										
					// delatnosti
					$delname = $this->db->getOne("SELECT delatnost_ime FROM delatnosti WHERE delatnost_id=".$v["delatnost"]." LIMIT 1");
					$delatnosti_names[$v["delatnost"]] = $delname?$delname:"NEMA ".$v["delatnost"];
					$delatnost[$k] = $delname;
					if (isset($delatnosti[$v["delatnost"]])) {
						$delatnosti[$v["delatnost"]] += count($v["artikli"])*$v["kolicina"];
					}else{
						$delatnosti[$v["delatnost"]] = count($v["artikli"])*$v["kolicina"];
					}
					
					// artikli
					$artikl[$k] = "<ol>";
					foreach ($v["artikli"] as $artkey=>$artid){
						
						$mods = $v["artikli_mods"][$artkey];
						
						$artikl[$k] .= "<li>".$_artikli[$artid]."</li>";
						if (isset($artikli[$artid])) {
							$artikli[$artid] += (int)$v["kolicina"];
						}else{
							$artikli[$artid] = (int)$v["kolicina"];
						}
						
						// artikli-ugovori-detalji
						$artikli_ugovori[$artid][$v["id"]]["broj"] = $v["broj"];
						$artikli_ugovori[$artid][$v["id"]]["id"] = $v["komitent"];
						$artikli_ugovori[$artid][$v["id"]]["name"] = $komname;
						if (isset($artikli_ugovori[$artid][$v["id"]])) {
							$artikli_ugovori[$artid][$v["id"]]["kolicina"] += (int)$v["kolicina"];
						}else{
							$artikli_ugovori[$artid][$v["id"]]["kolicina"] = (int)$v["kolicina"];
						}
						if (isset($artikli_ugovori[$artid][$v["id"]]["tipovi"][$mods["tip_id"]])) {
							$artikli_ugovori[$artid][$v["id"]]["tipovi"][$mods["tip_id"]] += (int)$v["kolicina"];
						}else{
							$artikli_ugovori[$artid][$v["id"]]["tipovi"][$mods["tip_id"]] = (int)$v["kolicina"];
						}
						if (isset($artikli_ugovori[$artid][$v["id"]]["boje"][$mods["boja_id"]])) {
							$artikli_ugovori[$artid][$v["id"]]["boje"][$mods["boja_id"]] += (int)$v["kolicina"];
						}else{
							$artikli_ugovori[$artid][$v["id"]]["boje"][$mods["boja_id"]] = (int)$v["kolicina"];
						}
						
						// artikli-mods-detalji-tip
						$mods["tip_id"] = (int)$mods["tip_id"];
						if(isset($artikli_modovi_tip[$artid][$mods["tip_id"]])){
							$artikli_modovi_tip[$artid][$mods["tip_id"]] += (int)$v["kolicina"];
						}else{
							$artikli_modovi_tip[$artid][$mods["tip_id"]] = (int)$v["kolicina"];
						}
						
						// artikli-mods-detalji-boja
						$mods["boja_id"] = (int)$mods["boja_id"];
						if(isset($artikli_modovi_boja[$artid][$mods["boja_id"]])){
							$artikli_modovi_boja[$artid][$mods["boja_id"]] += (int)$v["kolicina"];
						}else{
							$artikli_modovi_boja[$artid][$mods["boja_id"]] = (int)$v["kolicina"];
						}
						
						// artikli-delatnosti-detalji
						if (isset($delatnosti_artikli[$v["delatnost"]][$artid])) {
							$delatnosti_artikli[$v["delatnost"]][$artid] += (int)$v["kolicina"];
						}else{
							$delatnosti_artikli[$v["delatnost"]][$artid] = (int)$v["kolicina"];
						}
						
					}
					$artikl[$k] .= "</ol>";
					$kolicina[$k] = $v["kolicina"];
					
				}
				
				$ugovori = array_unique($ugovori);
				$komitenti = array_unique($komitenti);
				
				$this->assign("sum_ugovora",number_format(count($ugovori),0,",","."));
				$this->assign("sum_komitenata",number_format(count($komitenti),0,",","."));
				
				$this->assign("ugovorid",$ugovorid);
				$this->assign("ugovorbroj",$ugovorbroj);
				$this->assign("komitentid",$komitentid);
				$this->assign("komitentname",$komitentname);
				$this->assign("artikl",$artikl);
				$this->assign("kolicina",$kolicina);
				$this->assign("delatnost",$delatnost);
				
				//echo count($artikli_ugovori);
				
				// po artiklima
				natsort($artikli);
				$artikli =  array_reverse($artikli,true);
				$pagesum=$artsum=0;
				foreach ($artikli as $artid=>$num){
					$artname[] = $_artikli[$artid];
					$artnum[] = $num;
					
					// detalji-ugovori
					$artugo = "<table>"
							."<tr class=\"head\" align=\"center\">"
							."<td>#</td>"
							."<td>ugovor</td>"
							."<td colspan=\"2\">komitent</td>"
							."<td>kol.</td>"
							."<td>tip</td>"
							."<td>boja</td>"
							."</tr>";
					$i=0;
					foreach ($artikli_ugovori[$artid] as $ugovor=>$aud){
						$artugo .= "<tr>"
								."<td>".(++$i)."</td>"
								."<td>".$aud["broj"]."</td>"
								."<td>".$aud["id"]."</td>"
								."<td>".$aud["name"]."</td>"
								."<td>".$aud["kolicina"]."</td>"
								."<td>";
								foreach ($aud["tipovi"] as $audtipid=>$audtipnum){
									//$artugo .= $_artikli_tipovi[$audtipid].": $audtipnum<br />";
									$artugo .= $_artikli_tipovi[$audtipid].", ";
								}
								$artugo = preg_replace('/,\s*$/',"",$artugo);
								$artugo .= "</td><td>";
								foreach ($aud["boje"] as $audbojaid=>$audbojanum){
									//$artugo .= $_artikli_boje[$audbojaid].": $audbojanum<br />";
									$artugo .= $_artikli_boje[$audbojaid].", ";
								}
								$artugo = preg_replace('/,\s*$/',"",$artugo);
								$artugo .= "</td>";
					}
					$artugo .= "</table>";
					$artugovori[] = htmlentities(preg_replace('/\n/'," ",$artugo),2,"UTF-8");
					
					// detalji-mods
					$amod = "<table>"
							."<tr class=\"head\" align=\"center\">"
							."<td>tip</td>"
							."<td>količina</td>"
							."</tr>";
					foreach ($artikli_modovi_tip[$artid] as $idtip=>$numtip){
						$amod .= "<tr>"
								."<td>".$_artikli_tipovi[$idtip]."</td>"
								."<td>$numtip</td>";
					}
					$amod .= "</table>";
					$amod .= "<table>"
							."<tr class=\"head\" align=\"center\">"
							."<td>boja</td>"
							."<td>količina</td>"
							."</tr>";
					foreach ($artikli_modovi_boja[$artid] as $idboja=>$numboja){
						$amod .= "<tr>"
								."<td>".$_artikli_boje[$idboja]."</td>"
								."<td>$numboja</td>";
					}
					$amod .= "</table>";
					$artmods[] = htmlentities(preg_replace('/\n/'," ",$amod),2,"UTF-8");
					
					$artsum += $num;
					
					$koeficijent = $_artikli_koeficijenti[$artid];
					$pn = (float)$koeficijent*$num;
					$pagesum+=$pn;
					$pagenum[] = $pn>0?number_format($pn,4,",","."):"";
				}
				$this->assign("broj_artikala",count($artname));
				$this->assign("artname",$artname);
				$this->assign("artnum",$artnum);
				$this->assign("artugovori",$artugovori);
				$this->assign("artmods",$artmods);
				$this->assign("artsum",$artsum);
				$this->assign("pagenum",$pagenum);
				$this->assign("pagesum",number_format($pagesum,4,",","."));
				
				
				// po delatnostima
				$delsum=0;
				natsort($delatnosti);
				$delatnosti = array_reverse($delatnosti,true);
				foreach ($delatnosti as $delid=>$delnum){
					$delids[] = $delid;
					$delnums[] = $delnum;
					$delnames[] = $delatnosti_names[$delid];
					
					$da_html = "";
					if (isset($delatnosti_artikli[$delid])) {
						$da_html .= "<table cellspacing=\"0\">";
						natsort($delatnosti_artikli[$delid]);
						$delatnosti_artikli[$delid] = array_reverse($delatnosti_artikli[$delid],true);
						foreach ($delatnosti_artikli[$delid] as $da_artid=>$da_artnum){
							$da_html .= "<tr>"
									   ."<td>".$_artikli[$da_artid]."</td>"
									   ."<td align=\"right\" width=\"10%\">$da_artnum</td>"
									   ."</tr>";
						}
						$da_html .= "</table>";
					}
					$delarts[] = $da_html;
					
					$delsum += $delnum;
				}
				$this->assign("broj_delatnosti",count($delids));
				$this->assign("delid",$delids);
				$this->assign("delname",$delnames);
				$this->assign("delnum",$delnums);
				$this->assign("delarts",$delarts);
				$this->assign("delsum",$delsum);
				
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema rezultata.");
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Odaberite kampanju.");
		}
		
		
		
		echo $this->parseTemplate("ugovori/reports/book.tpl");
	}
	
	//-------------------------------------------------------------
	
	function DebtReport(){
		$day = 60*60*24;
		
		// get date uplate
		$filter_uplata_start = getFromInput("filter_date_uplate_od");
		$this->assign("filter_date_uplate_od",$filter_uplata_start);
		$filter_uplata_start = is_valid_date($filter_uplata_start)?date2int($filter_uplata_start):0;
		
		$filter_uplata_end = getFromInput("filter_date_uplate_do");
		$this->assign("filter_date_uplate_do",$filter_uplata_end);
		$filter_uplata_end = is_valid_date($filter_uplata_end)?date2int($filter_uplata_end):0;
		
		
		// get date ugovor
		$filter_ugovor_start = getFromInput("filter_date_ugovor_od");
		$this->assign("filter_date_ugovor_od",$filter_ugovor_start);
		$filter_ugovor_start = is_valid_date($filter_ugovor_start)?date2int($filter_ugovor_start):0;
		
		$filter_ugovor_end = getFromInput("filter_date_ugovor_do");
		$this->assign("filter_date_ugovor_do",$filter_ugovor_end);
		$filter_ugovor_end = is_valid_date($filter_ugovor_end)?date2int($filter_ugovor_end):0;
		
		// get kampanja
		$filter_kampanja = getFromInput("filter_kampanja");
		$polja = $this->get_Kampanje($filter_kampanja);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("selected_filter_kampanja",$polja["polje_selected"]);
			
		// get radnik
		$filter_radnik = getFromInput("filter_radnik");
		$polja = $this->get_Radnici($filter_radnik);
			$this->assign("filter_radnik_id",$polja["polje_id"]);
			$this->assign("filter_radnik_ime",$polja["polje_ime"]);
			$this->assign("filter_radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected_filter",$polja["polje_selected"]);
			
		// get delatnost
		$filter_delatnost = getFromInput("filter_delatnost");
		$polja = $this->get_DelatnostiAll($filter_delatnost);
			$this->assign("filter_delatnost_id",$polja["polje_id"]);
			$this->assign("filter_delatnost_name",$polja["polje_ime"]);
			$this->assign("filter_delatnost_selected",$polja["polje_selected"]);
			
		// get aktivni radnici
		$filter_active = getFromInput("filter_active");
		$filter_active = $filter_active=="on"?1:0;
		$this->assign("checked_filter_active",$filter_active>0?" checked":"");
		
		// get storno
		$filter_storno = getFromInput("filter_storno");
		$filter_storno= $filter_storno=="on"?1:0;
		$this->assign("checked_filter_storno",$filter_storno>0?" checked":"");
			
		$ima_uplata = ($filter_uplata_start>0 && $filter_uplata_end>0);
		$ima_ugovor = ($filter_ugovor_start && $filter_ugovor_end);
		if ($ima_uplata || $ima_ugovor || $filter_kampanja>0) {
			$q_select = "SELECT ugovori.id, ugovori.broj, ugovori.datum, ugovori.komitent as ugokom, radnici.radnik_ime, radnici.radnik_prezime, ugovori.saradnik, ugovori.iznos_neto, ugovori.iznos_popust, radnici.radnik_ugo_end";
			$q_from = "FROM ugovori, radnici";
			$q_where = "WHERE (ugovori.saradnik=radnici.radnik_id) ";
			if ($ima_uplata) {
				$q_select .= ", ugovori_otplate.iznosbp, ugovori_otplate.datum as otplata_date, ugovori_otplate.izvod";
				$q_from .= ", 	ugovori_otplate";
				$q_where .= " AND ( (ugovori.id=ugovori_otplate.ugoid)";
				$q_where .= " AND (ugovori_otplate.datum>=$filter_uplata_start AND ugovori_otplate.datum<".($filter_uplata_end+$day).") )";
			}
			if ($ima_ugovor) {
				$qds = int2date($filter_ugovor_start,"Y-m-d");
				$qde = int2date($filter_ugovor_end+$day,"Y-m-d");
				$q_where .= " AND (ugovori.datum>='$qds' AND ugovori.datum<'$qde')";
			}
			$imakampanja=false;
			if ($filter_kampanja>0) {
				$imakampanja=true;
				$q_select .= ", ugovori_stavke.ugovor_id as StavkaUgovor";
				$q_from .= ", ugovori_stavke";
				$q_where .= " AND ( (ugovori_stavke.ugovor_id=ugovori.id)";
				$q_where .= " AND (ugovori_stavke.kampanja=$filter_kampanja))";
				if (!$ima_uplata) {
					$q_group = "GROUP BY StavkaUgovor";
				}
			}
			if($filter_radnik>0){
				$q_where .= "AND (ugovori.saradnik=$filter_radnik)";
			}
			if ($filter_delatnost>0) {
				if (!$imakampanja) {
					$q_select .= ", ugovori_stavke.ugovor_id as StavkaUgovor";
					$q_from .= ", ugovori_stavke";
					$q_where .= " AND (ugovori_stavke.ugovor_id=ugovori.id)";
					if (!$ima_uplata) {
						$q_group = "GROUP BY StavkaUgovor";
					}
				}
				$q_where .= " AND (ugovori_stavke.delatnost=$filter_delatnost)";
			}
			if ($filter_active>0) {
				$q_where .= " AND radnici.radnik_akt=1";
			}
			if ($filter_storno>0) {
				$q_where .= " AND ugovori.tip_ugovora!=5";
			}
			
			$q = "$q_select $q_from $q_where $q_group ORDER BY radnici.radnik_prezime, radnici.radnik_ime, ugovori.broj";
			//echo $q;
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $k=>$v){
					$ugovor_date = date2int(datetime2str($v["datum"]));
					$otplata_date = intval($v["otplata_date"]);
					
					// prepravka popusta stavki
					// ako je kolicina=0 popust ne oduzima od ukupne cene
					$popust = ugovori::getUgovorPopust($v["id"]);
					//$zak_neto = floatval($v["iznos_neto"])-floatval($v["iznos_popust"]);
					$zak_neto = (float)$v["iznos_neto"] - (float)$popust;
					
					$data[$v["saradnik"]][$v["broj"]]["zakljuceno"][] = $zak_neto;
					
					if ($ima_uplata) {
						$data[$v["saradnik"]][$v["broj"]]["naplaceno"][] = $v["iznosbp"];
					}else{
						$naplaceno_d = (float) $this->db->getOne("SELECT SUM(iznosbp) FROM ugovori_otplate WHERE ugoid=".$v["id"]);
						$data[$v["saradnik"]][$v["broj"]]["naplaceno"][] = $naplaceno_d;
					}
					
					$radnici_data[$v["saradnik"]]["ime"] = $v["radnik_ime"];
					$radnici_data[$v["saradnik"]]["prezime"] = $v["radnik_prezime"];
					$radnici_data[$v["saradnik"]]["ugovor_end"] = $v["radnik_ugo_end"];
					
					//$vrednost[$k] = number_format($v["iznos_bruto"],2,",",".");
					//$iznos[$k] =isset($v["iznos"])>0?number_format($v["iznos"],2,",","."):"";
					
					$details_radnik[$k] = $v["saradnik"];
					$details_komid[$k] = $v["ugokom"];
					$details_komname[$k] = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$v["ugokom"]." LIMIT 1");
					$details_ugovor[$k] = $v["broj"];
					$details_ugovor_id[$k] = $v["id"];
					$details_zaklj[$k] = number_format($zak_neto,2,",",".");
					
					if ($ima_uplata) {
						$details_napla[$k] = number_format($v["iznosbp"],2,",",".");
						if (strlen(trim($v["izvod"]))==0) {
							$details_cash_uplata[$k] = " cash";
						}
					}else{
						$details_napla[$k] = number_format($naplaceno_d,2,",",".");
					}
					
					$details_ugovor_date[$k] = int2date($ugovor_date);
					$details_otp_date[$k] = $otplata_date>0?int2date($otplata_date):"";
					
					if ($otplata_date>intval($v["radnik_ugo_end"]) && intval($v["radnik_ugo_end"])>0) {
						$details_class_overflow[$k] = " overflow";
					}else{
						$details_class_overflow[$k] = "";
					}
				}
				$this->assign("info","Ukupno: ".count($data)." radnika.");
				$sum_ugo = $sum_zaklj = $sum_napla = 0;
				//print_rarray($data);
				foreach ($data as $radid=>$v){
					$radnik_id[] = $radid;
					//$radnik[] = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$radid LIMIT 1");
					//$radnik[] = $radnici_data[$radid]["ime"]." ".$radnici_data[$radid]["prezime"];
					$radnik[] = $radnici_data[$radid]["prezime"]." ".$radnici_data[$radid]["ime"];
					$ugoend = $radnici_data[$radid]["ugovor_end"];
					$radnik_ugo_end[] = $ugoend>0?int2date($ugoend):"";
					
					$ugos = array_keys($v);
					$num_ugovora[] = count($ugos);
					$zaklj = $napla = 0;
					foreach ($v as $ugoid=>$m){
						$zaklj += $m["zakljuceno"][0];
						$napla += array_sum($m["naplaceno"]);
					}
					$vrednost[] = number_format($zaklj,2,",",".");
					$iznos[] = number_format($napla,2,",",".");
					
					$sum_ugo += count($v);
					$sum_zaklj += $zaklj;
					$sum_napla += $napla;
				}
				
				$this->assign("radnik_id",$radnik_id);
				$this->assign("radnik",$radnik);
				$this->assign("radnik_ugovor_end",$radnik_ugo_end);
				$this->assign("num_ugovora",$num_ugovora);
				$this->assign("zakljuceno",$vrednost);
				$this->assign("naplaceno",$iznos);
				
				$this->assign("details_radnik",$details_radnik);
				$this->assign("details_komid",$details_komid);
				$this->assign("details_komname",$details_komname);
				$this->assign("details_ugovor",$details_ugovor);
				$this->assign("details_ugovor_id",$details_ugovor_id);
				$this->assign("details_zakljuceno",$details_zaklj);
				$this->assign("details_naplaceno",$details_napla);
				$this->assign("class_cash",$details_cash_uplata);
				$this->assign("class_overflow",$details_class_overflow);
				$this->assign("details_ugovor_date",$details_ugovor_date);
				$this->assign("details_otplata_date",$details_otp_date);
				
				$this->assign("sum_ugo",$sum_ugo);
				$this->assign("sum_zaklj",number_format($sum_zaklj,2,",","."));
				$this->assign("sum_napla",number_format($sum_napla,2,",","."));
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->");
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
		}
		
		echo $this->parseTemplate("ugovori/reports/debt.tpl");
	}
	function DebtReportDugovanja(){
		$day = 60*60*24;
		$display_result = "none";
		// get kampanja
		$filter_kampanja = getFromInput("filter_kampanja");
		$polja = $this->get_Kampanje($filter_kampanja);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("selected_filter_kampanja",$polja["polje_selected"]);
		
		// get radnik
		$filter_radnik = getFromInput("filter_radnik");
		$filter_radnik=$filter_radnik>0?$filter_radnik:null;
		$polja = $this->get_Radnici($filter_radnik);
			$this->assign("filter_radnik_id",$polja["polje_id"]);
			$this->assign("filter_radnik_ime",$polja["polje_ime"]);
			$this->assign("filter_radnik_prezime",$polja["polje_prezime"]);
			$this->assign("filter_radnik_selected",$polja["polje_selected"]);
		
		// get delatnost
		$filter_delatnost = getFromInput("filter_delatnost");
		$polja = $this->get_DelatnostiAll($filter_delatnost);
			$this->assign("filter_delatnost_id",$polja["polje_id"]);
			$this->assign("filter_delatnost_name",$polja["polje_ime"]);
			$this->assign("filter_delatnost_selected",$polja["polje_selected"]);
			
		//tip ugovora
		$filter_tip_ugovora = getFromInput("filter_tip_ugovora");
		$ugovori_tipovi=$this->get_UgovorTipovi($filter_tip_ugovora);
		$this->assign("filter_ugovori_tipovi_ids",$ugovori_tipovi["polje_id"]);
		$this->assign("filter_ugovori_tipovi",$ugovori_tipovi["polje_ime"]);
		$this->assign("selected_filter_ugovor_tip",$ugovori_tipovi["polje_selected"]);
		
		//pozivanje
		$filter_pozivanje=getFromInput("filter_ugovori_pozivanje");
		for ($radio_pozivanje=1;$radio_pozivanje<=3;$radio_pozivanje++)
			$this->assign("checked_pozivanje_radio$radio_pozivanje"," ");
		$radio_pozivanje=3;
		switch ($filter_pozivanje)
		{
			case "1":
				$radio_pozivanje=1;
				break;
			case "2":
				$radio_pozivanje=2;
				break;
		}
		$this->assign("checked_pozivanje_radio$radio_pozivanje"," checked ");
		$filter_pozivalac=getFromInput("filter_pozivalac");
		
		// get ugovor period
		$filter_date_ugo_start = getFromInput("filter_ugovor_od",true);
		$this->assign("filter_ugovor_od",$filter_date_ugo_start);
		$filter_date_ugo_start = is_valid_date($filter_date_ugo_start)?date2int($filter_date_ugo_start):0;
		
		$filter_date_ugo_end = getFromInput("filter_ugovor_do",true);
		$this->assign("filter_ugovor_do",$filter_date_ugo_end);
		$filter_date_ugo_end = is_valid_date($filter_date_ugo_end)?date2int($filter_date_ugo_end):0;
		
		// get plan period		
		$filter_date_plan_start = getFromInput("filter_plan_od",true);
		$this->assign("filter_plan_od",$filter_date_plan_start);
		$filter_date_plan_start = is_valid_date($filter_date_plan_start)?date2int($filter_date_plan_start):0;
		
		$filter_date_plan_end = getFromInput("filter_plan_do",true);
		$this->assign("filter_plan_do",$filter_date_plan_end);
		$filter_date_plan_end = is_valid_date($filter_date_plan_end)?date2int($filter_date_plan_end):0;
		
		$ima_ugovor = ($filter_date_ugo_start>0 && $filter_date_ugo_end>0 && $filter_date_ugo_end>=$filter_date_ugo_start);
		
		$ima_plan = ($filter_date_plan_start>0 && $filter_date_plan_end>0 && $filter_date_plan_end>=$filter_date_plan_start);
		
		if ($ima_ugovor || $ima_plan || $filter_radnik>0) {
			$display_result = "block";
			$q_select = "SELECT "
					."r.radnik_id, r.radnik_ime, r.radnik_prezime, "
					."k.komitent_id, k.komitent_name, k.komitent_vlasnik_name, k.komitent_direktor, k.komitent_predst, "
					."u.id, u.broj, u.iznos_bruto, u.datum, u.user_pozivalac,ut.name as 'ugo_tip_name'";
			$q_from = "FROM ugovori u, radnici r, komitenti k, ugovori_tipovi ut ";
			$q_where = "WHERE u.saradnik=r.radnik_id "
					  ." AND u.komitent=k.komitent_id "
					  ." AND u.tip_ugovora=ut.tip_id ";
			$q_group = "";
					
			if ($ima_ugovor) {
				$q_ugodate_start = date("Y-m-d",$filter_date_ugo_start);
				$q_ugodate_end = date("Y-m-d",$filter_date_ugo_end+$day);
				$q_where .= "AND (u.datum>='$q_ugodate_start' AND u.datum<'$q_ugodate_end') ";
			}
			if ($ima_plan) {
				$q_from .= ", ugovori_plan_otplate upo, ugovori_plan_rate upr ";
				$q_where .= "AND u.id=upo.ugovor_id AND upo.plan_id=upr.rata_plan ";
				$q_where .= "AND ("
									."(upr.datum>='$filter_date_plan_start' AND upr.datum<'$filter_date_plan_end') OR "
							   		."(upo.avans_date>='".date("Y-m-d",$filter_date_plan_start)."' AND upo.avans_date<'".date("Y-m-d",$filter_date_plan_end)."')"
						   		.") ";
				$q_group .= "GROUP BY u.id";
			}
			if ($filter_radnik>0) {
				$q_where .= " AND u.saradnik=$filter_radnik ";
			}
			$imakampanja=false;
			if ($filter_kampanja>0) {
				$imakampanja=true;
				$q_from .= ", ugovori_stavke us";
				$q_where .= " AND u.id=us.ugovor_id";
				$q_where .= " AND us.kampanja=$filter_kampanja";
			}
			if ($filter_delatnost>0) {
				if (!$imakampanja) {
					$q_from .= ", ugovori_stavke us";
					$q_where .= " AND u.id=us.ugovor_id";
				}
				$q_where .= " AND us.delatnost=$filter_delatnost";
			}

			//pozivanje
			if ($filter_pozivanje=="1")
				$q_where .= " AND not u.user_pozivalac is null";
			elseif ($filter_pozivanje=="2")
				$q_where .= " AND u.user_pozivalac is null";
			if ($filter_pozivalac>0)
				$q_where .= " AND u.user_pozivalac=$filter_pozivalac";

			//tip ugovora
			if ($filter_tip_ugovora>0)
				$q_where .= " AND ut.tip_id=$filter_tip_ugovora";
				
			$q = "$q_select $q_from $q_where $q_group ORDER BY u.datum";
			//echo $q; die();
			$assign = array("komid"=>array());
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $k=>$v){
					
					$q = "SELECT SUM(iznos) FROM ugovori_otplate WHERE ugoid=".$v["id"];
					$uplaceno = floatval($this->db->getOne($q));
					//echo "$q<br />";
										
					
					$minDug = $v["iznos_bruto"]*$this->debtMinPerc/100;
					$dug = floatval($v["iznos_bruto"])-$uplaceno;
					
					//echo $v["iznos_bruto"]." - $minDug<br />";
					
					if ($dug>$minDug) {
					//if (1) {
						$key = count($assign["komid"]);
						$assign["komid"][$key] = $v["komitent_id"];
						$assign["komname"][$key] = $v["komitent_name"];
						
						$q1 = "SELECT DISTINCT telefon FROM komitenti_telefoni WHERE fax=0 AND kom_id=".$v["komitent_id"]." ORDER BY `order`"; 
						$phones = $this->db->select2($q1);
						$ph="";
						if ($phones!==false) {
							foreach ($phones as $phone){
								$ph .= "\n".$phone["telefon"];
							}
							$ph = substr($ph,1);
						}
						$assign["komphone"][$key] = $ph;
						
						$contacts = "vlasnik: ".$v["komitent_vlasnik_name"]."\n"
									."direktor: ".$v["komitent_direktor"]."\n"
									."predstavnik: ".$v["komitent_predst"];
						$assign["komcontacts"][$key] = $contacts;
						$assign["radnik_ime"][$key] = $v["radnik_ime"];
						$assign["radnik_prezime"][$key] = $v["radnik_prezime"];
						$assign["ugobroj"][$key] = $v["broj"];
						$assign["ugoiznos"][$key] = number_format($v["iznos_bruto"],2,",",".");
						$assign["ugootplata"][$key] = number_format($uplaceno,2,",",".");
						$assign["ugodug"][$key] = number_format($dug,2,",",".");
						$assign["ugodate"][$key] = datetime2str($v["datum"]);
						$assign["pozivalac_user_id"][$key]	= $v["user_pozivalac"];
						$assign["ugovor_tip"][$key] = $v["ugo_tip_name"];
						$assign["ugovori_ids"][$key] = $v["id"];

						$sum[$v["radnik_id"]]["vrednost"][] = $v["iznos_bruto"];
						$sum[$v["radnik_id"]]["uplaceno"][] = $uplaceno;
						$sum[$v["radnik_id"]]["duguje"][] = $dug;
						$sum[$v["radnik_id"]]["rad_ime"] = $v["radnik_ime"];
						$sum[$v["radnik_id"]]["rad_prezime"] = $v["radnik_prezime"];
						
						list($planid,$brrata,$avans,$avans_date) = $this->db->getRow("SELECT plan_id, broj_rata, avans_bruto, avans_date FROM ugovori_plan_otplate WHERE ugovor_id=".$v["id"]." LIMIT 1");
						
						$q1 = "SELECT bruto, datum "
							."FROM ugovori_plan_rate "
							."WHERE rata_plan=$planid "
							."ORDER BY datum";
						$plans = $this->db->select2($q1);
						$pl = "<table><tr><td>AVANS:</td><td title=\"".datetime2str($avans_date)."\">".number_format($avans,2,",",".")."</td></tr>";
						$pl_last = "";
						if ($plans!==false && count($plans)>0) {
							if ($avans>0 && $brrata<count($plans)) {
								array_splice($plans,0,1);
							}
							foreach ($plans as $plan){
								$pl .= "<tr><td>".int2date($plan["datum"])."</td><td>".number_format($plan["bruto"],2,",",".")."</td></tr>";
							}
							$pl_last = int2date($plans[count($plans)-1]["datum"]);
						}else{
							$ad = datetime2str($avans_date);
							$pl_last = date2int($ad)>0?$ad:"";
						}
						$assign["ugoplan"][$key] = "$pl</table>";
						$assign["ugoplan_last"][$key] = $pl_last;
					}
				}
							
				//svi aktivni radnici
				$q="select u.id as user_id,r.radnik_ime,r.radnik_prezime";
				$q.=" from radnici r,users u";
				$q.=" where r.radnik_akt=1 and r.radnik_id=u.radnik_id";
				$q.=" order by r.radnik_ime,r.radnik_prezime";
				$res=$this->db->select2($q);
				if ($res)
				{
					foreach ($res as $k=>$v)
					{
						$radnici[$v["user_id"]]=$v["radnik_ime"]." ".$v["radnik_prezime"];
						$radnici_svi[]=$v["radnik_ime"]." ".$v["radnik_prezime"];
						$users_id[]=$v["user_id"];
					}
					$this->assign("users_id",$users_id);
					$this->assign("radnici_svi",$radnici_svi);
				}
				//prepare pozivaoci
				$pozivaoci_user_ids=array_unique($assign["pozivalac_user_id"]);
				array_removeEmptyItems($pozivaoci_user_ids);
				foreach ($pozivaoci_user_ids as $userid)
				{
					$filter_pozivaoci_user_ids[]=$userid;
					$filter_pozivaoci_users[]=$radnici[$userid];
					if ($userid==$filter_pozivalac)
						$pozivaoci_selected[]="selected";
					else $pozivaoci_selected[]=" ";
				}
				$this->assign("filter_pozivaoci_user_ids",$filter_pozivaoci_user_ids);
				$this->assign("filter_pozivaoci_users",$filter_pozivaoci_users);
				$this->assign("filter_pozivaoci_selected",$pozivaoci_selected);
				foreach ($assign["pozivalac_user_id"] as $key=>$userid)
				{
					$assign["pozivalac"][$key]=$radnici[$userid];
				}
				//
				//
				//echo "<pre>";var_dump($assign["ugoplan_last"]);echo "</pre>";
				//print_rarray($assign);
				foreach ($assign as $k=>$v){
					$this->assign($k,$v);
				}
				$sum_vrednost = $sum_uplaceno = $sum_duguje = 0;
				if (count($sum)>0) {
					foreach ($sum as $radid=>$data){
						$radnik_sum[] = $radid;
						$radnik_ime_sum[] = $data["rad_ime"];
						$radnik_prezime_sum[] = $data["rad_prezime"];
						
						$vrednost = array_sum($data["vrednost"]); $sum_vrednost += $vrednost;
						$vrednost_sum[] = number_format($vrednost,2,",",".");
						
						$uplaceno = array_sum($data["uplaceno"]); $sum_uplaceno += $uplaceno;
						$uplaceno_sum[] = number_format($uplaceno,2,",",".");
						
						$duguje = array_sum($data["duguje"]); $sum_duguje += $duguje;
						$duguje_sum[] = number_format($duguje,2,",",".");
					}
				}
				$this->assign("radnik_sum",$radnik_sum);
				$this->assign("radnik_ime_sum",$radnik_ime_sum);
				$this->assign("radnik_prezime_sum",$radnik_prezime_sum);
				$this->assign("vrednost_sum",$vrednost_sum);
				$this->assign("uplaceno_sum",$uplaceno_sum);
				$this->assign("duguje_sum",$duguje_sum);
				$this->assign("sum_vrednost",number_format($sum_vrednost,2,",","."));
				$this->assign("sum_uplaceno",number_format($sum_uplaceno,2,",","."));
				$this->assign("sum_duguje",number_format($sum_duguje,2,",","."));
				
				$this->assign("info","Ukupno: ".count($assign["komid"]));
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema rezultata");
			}
		}
		
		$this->assign("display_result",$display_result);
		
		echo $this->parseTemplate("ugovori/reports/debt_dugovanja.tpl");
	}
	
	//-------------------------------------------------------------
	
	function get_UgovorStavke($ugoid){
		$q = "SELECT * FROM ugovori_stavke WHERE ugovor_id=$ugoid";
		$res = $this->db->select2($q);
		
		if ($res !== false) {
			foreach ($res as $k=>$stavka){
				if ($stavka["paket"]>0) {
					$art = $this->get_CenePaket($stavka["artikl"]);
					$art = $art["name"];
				}else{
					$art = $this->get_CeneArtikl($stavka["artikl"]);
					$art = implode(" / ",$art["art_full_name"]);
				}
				$ret[$k] = $art;
			}
		}
		
		return $ret;
	}

	function duzniciSavePozivalac($ugovor,$user)
	{
		if (strlen($ugovor)==0)
		{
			echo "Navedite ugovor";
			return;
		}
		if (strlen($user)==0)
			$user="null";
		$q="update ugovori set user_pozivalac=$user where id=$ugovor";
		$res=$this->db->alterTable($q);
		if (!$res || $res==0)
		{
			echo "Neuspelo aÅ¾uriranje.";
			return;
		}
		$q="insert into duznici_pozivaoci(ugovor_id,user_caller,creator_time)";
		$q.=" values($ugovor,$user,".time().")";
		$res=$this->db->alterTable($q);
		if (!$res)
		{
			echo "Neuspelo aÅ¾uriranje.";
			return;
		}
		echo "Izmenjen pozivalac.";
	}
}

?>